package com.github.lee.unsplash.ui.main

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.annotation.IntRange
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.viewpager2.widget.ViewPager2
import com.github.lee.core.common.normalIntent
import com.github.lee.mvvm.core.ui.BaseActivity
import com.github.lee.mvvm.core.vm.BaseViewModel
import com.github.lee.unsplash.R
import com.github.lee.unsplash.databinding.ActivityMainBinding
import com.github.lee.unsplash.databinding.LayoutHomeTabItemBinding
import com.google.android.material.animation.ArgbEvaluatorCompat
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, BaseViewModel>() {

    private val mediator by lazy {
        TabLayoutMediator(
            pageVB!!.tlMainTabs, pageVB!!.vpMainContent
        ) { tab, position -> tab.customView = customTabView(position) }
    }

    companion object {
        private const val KEY_POSITION = "POSITION"
        fun start(context: Context, @IntRange(from = 0, to = 2) position: Int = 0) {
            context.startActivity(normalIntent(context, MainActivity::class.java).also {
                it.putExtras(bundleOf(KEY_POSITION to position))
            })
        }
    }

    //===Desc:=========================================================
    override fun onCreateViewModel(): BaseViewModel = viewModels<BaseViewModel>().value
    override fun onCreateSuccess(): ActivityMainBinding =
        ActivityMainBinding.inflate(layoutInflater)

    //===Desc:=========================================================

    override fun onObserveData() {
        super.onObserveData()
        pageVM.apply {

        }
    }

    override fun onSetViewData() {
        super.onSetViewData()
        translucentStatusBar()
        pageVB?.apply {
            vpMainContent.adapter = MainPageAdapter(this@MainActivity)

         //ViewPager滚动 tab颜色渐变
            vpMainContent.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                val selectColor = Color.parseColor("#5075FC")
                val normalColor = Color.parseColor("#999999")
                val evaluator = ArgbEvaluatorCompat.getInstance()

                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                    val currentPosition = vpMainContent.currentItem
                    if (currentPosition == position) {
                        val currentTab = tlMainTabs.getTabAt(position)?.customView
                        val currentTabImg = currentTab?.findViewById<ImageView>(R.id.iv_img)
                        val currentShowColor = evaluator.evaluate(
                            positionOffset,
                            selectColor, normalColor
                        )
                        currentTabImg?.drawable?.setTint(currentShowColor)

                        val nextTab = tlMainTabs.getTabAt(position + 1)?.customView
                        val nextTabImg = nextTab?.findViewById<ImageView>(R.id.iv_img)
                        val nextShowColor = evaluator.evaluate(
                            positionOffset,
                            normalColor, selectColor
                        )
                        nextTabImg?.drawable?.setTint(nextShowColor)
                    } else {
                        val currentTab = tlMainTabs.getTabAt(currentPosition)?.customView
                        val currentTabImg = currentTab?.findViewById<ImageView>(R.id.iv_img)
                        val currentShowColor = evaluator.evaluate(
                            positionOffset,
                            normalColor, selectColor
                        )
                        currentTabImg?.drawable?.setTint(currentShowColor)

                        val nextTab = tlMainTabs.getTabAt(position)?.customView
                        val nextTabImg = nextTab?.findViewById<ImageView>(R.id.iv_img)
                        val nextShowColor = evaluator.evaluate(
                            positionOffset,
                            selectColor, normalColor
                        )
                        nextTabImg?.drawable?.setTint(nextShowColor)
                    }
                }
            })
            mediator.attach()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mediator.detach()
    }

    //===Desc:=========================================================

    private fun customTabView(position: Int): View {
        return LayoutHomeTabItemBinding.inflate(layoutInflater)
            .also {
                it.ivImg.setImageDrawable(
                    ContextCompat.getDrawable(
                        mContext, when (position) {
                            0 -> R.drawable.ic_baseline_home_24
                            1 -> R.drawable.ic_baseline_topic_24
                            else -> R.drawable.ic_baseline_person_24
                        }
                    )
                )
            }.root
    }

}